package com.brush.brushcommon.common.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brush.brushcommon.common.basic.mapper.SystemQuestionBankMapper;
import com.brush.brushcommon.common.basic.pojo.BankOfTopic;
import com.brush.brushcommon.common.basic.pojo.SystemQuestionBank;
import com.brush.brushcommon.common.basic.service.BankOfTopicService;
import com.brush.brushcommon.common.basic.service.SystemQuestionBankService;
import com.brush.brushcommon.dto.Result;
import com.brush.brushcommon.dto.basic.SystemQuestionBankFDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;

import static com.brush.brushcommon.common.mq.ExchangeConstants.*;

/**
 * <p>
 * 系统题库 服务实现类
 * </p>
 *
 * @author debug
 * @since 2024-07-14
 */
@Service
public class SystemQuestionBankServiceImpl extends ServiceImpl<SystemQuestionBankMapper, SystemQuestionBank> implements SystemQuestionBankService {

    @Resource
    private SystemQuestionBankMapper systemQuestionBankMapper;


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Result selectQuestionBankList(SystemQuestionBankFDto systemQuestionBankFDto) {
        LambdaQueryWrapper<SystemQuestionBank> systemQuestionBankQueryWrapper = new LambdaQueryWrapper<>();
        systemQuestionBankQueryWrapper.like(StringUtils.isNoneEmpty(systemQuestionBankFDto.getSysQuestionBankName()),
                SystemQuestionBank::getSysQuestionBankName,
                systemQuestionBankFDto.getSysQuestionBankName()
        );
        systemQuestionBankQueryWrapper.eq(SystemQuestionBank::getIsShow,
                systemQuestionBankFDto.getIsShow()
        ).eq(SystemQuestionBank::getIsDeleted, 0);
        Page<SystemQuestionBank> page = new Page<>(systemQuestionBankFDto.getPageNum(), systemQuestionBankFDto.getPageSize());
        Page<SystemQuestionBank> res = systemQuestionBankMapper.selectPage(page, systemQuestionBankQueryWrapper);
        return Result.successPage(res);
    }

    /**
     * 添加题库
     *
     * @param systemQuestionBank
     * @return
     */
    @Override
    public Result addQuestionBank(SystemQuestionBank systemQuestionBank) {
        if (systemQuestionBankMapper.selectCount(new LambdaQueryWrapper<SystemQuestionBank>().
                eq(SystemQuestionBank::getSysQuestionBankName, systemQuestionBank.getSysQuestionBankName())) > 0
        ) {
            return Result.fail(500, "题库名称重复");
        }
//        TODO 分享链接等待前台小程序配置后完善
        if (systemQuestionBankMapper.insert(systemQuestionBank) > 0) {
            return Result.success("添加成功");
        }
        return Result.fail();
    }

    @Override
    public Result updateQuestionBank(SystemQuestionBank systemQuestionBank) {
        if (systemQuestionBankMapper.selectCount(new LambdaQueryWrapper<SystemQuestionBank>().
                eq(SystemQuestionBank::getSysQuestionBankName, systemQuestionBank.getSysQuestionBankName())) > 0
        ) {
            return Result.fail(500, "题库名称重复");
        }
        if (systemQuestionBankMapper.updateById(systemQuestionBank) > 0) {
            return Result.success("修改成功");
        }
        return null;
    }

    @Override
    public Result deleteQuestionBank(List<String> ids) {
        if (ids==null || ids.size() == 0) {
            return Result.fail(500, "ids不能为空");
        }
        //  删除题库
        boolean res = super.removeBatchByIds(ids);
        if(res){
        // 异步处理删除绑定的id  发送消息
        rabbitTemplate.convertAndSend(EXCHANGE_DELETE_QUESTIONBANK, ROUTING_KEY_DELETE_QUESTIONBANK, ids);
        }
        return Result.success();
    }
}
